package net.sansi.v3correctserver.service.show.impl;

import lombok.extern.slf4j.Slf4j;
import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.dto.v3serverapi.V3ServerCorrectDto;
import net.sansi.v3correctserver.entity.IntPoint;
import net.sansi.v3correctserver.entity.screen.CorrectArea;
import net.sansi.v3correctserver.entity.screen.Rect;
import net.sansi.v3correctserver.entity.space.CorrectCnf;
import net.sansi.v3correctserver.service.show.HardwareShowService;
import net.sansi.v3correctserver.service.show.ShowService;
import net.sansi.v3correctserver.service.show.SoftwareShowService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by Fang
 * create time : 2025/2/11
 * IDE : IntelliJ IDEA
 */
@Service
@Slf4j
public class ShowServiceImpl implements ShowService {

    @Resource
    private SoftwareShowService softwareShowService;
    @Resource
    private HardwareShowService hardwareShowService;

    @Override
    public V3ServerCorrectDto.HardwarePreviewEnable hardwarePreviewEnable(String screenId) {
        return hardwareShowService.hardwarePreviewEnable(screenId);
    }

    /**
     * 打屏
     *
     * @param area     区域
     * @param bright   亮度
     * @param colorIdx 颜色索引
     * @param mode     打屏模式
     *                 参考 {@link CorrectCnf#SHOW_MODE_AUTO}
     * @param span     隔点数量
     */
    @Override
    public ShowClose show(CorrectArea area, int bright, int colorIdx, int mode, int span, int ofx, int ofy) {
        switch (mode) {
            case CorrectCnf.SHOW_MODE_AUTO:
                return show(area, bright, colorIdx, autoMode(mode), span, ofx, ofy);
            case CorrectCnf.SHOW_MODE_SOFTWARE:
                return softwareShowService.show(area, bright, colorIdx, span, ofx, ofy);
            case CorrectCnf.SHOW_MODE_HARDWARE:
                return hardwareShowService.show(area, bright, colorIdx, span, ofx, ofy);
            default:
                return null;
        }
    }

    /**
     * 自动模式判定
     *
     * @return 打屏模式 1 | 2 {@link CorrectCnf#SHOW_MODE_SOFTWARE}
     */
    private int autoMode(int mode) {
        if (mode != CorrectCnf.SHOW_MODE_AUTO) return mode;
        // TODO 自动打屏判定
        return 1;
    }

    /**
     * 区域
     *
     * @param area 区域
     */
    @Override
    public ShowClose show(CorrectArea area, int bright, int colorIdx, int ofx, int ofy) {
        CorrectCnf cnf = AppHelper.projectSpace().getCorrectCnf();
        return show(area, bright, colorIdx, cnf.getShowMode(), cnf.getShowSpan(), ofx, ofy);
    }

    /**
     * 黑屏
     */
    @Override
    public ShowClose showBlack() {
        CorrectArea area = new CorrectArea();
        area.setAreaId(-1);
        area.setShowArea(new Rect(0, 0, new IntPoint(0, 0)));
        area.setValidArea(new Rect(0, 0, new IntPoint(0, 0)));
        return show(area, 0, 0, 0, 0);
    }

    /**
     * 关闭打屏
     */
    public void closeDisplay() {
        softwareShowService.close();
        hardwareShowService.close();
    }
}
